#include "portmanage.h"
#include "MsgParser.h"
#include "mylogger.h"
#include "gtime.h"

using namespace ns_yht;

PortManage::PortManage(QObject *parent) : QObject(parent)
{
    m_timerId = startTimer(100);
}

PortManage::~PortManage()
{
    if (m_timerId > 0)
        killTimer(m_timerId);
}

void PortManage::timerEvent(QTimerEvent *event)
{
    m_runtimes++;
    // if (m_runtimes % 10 == 0)
    // MyLogger::developInfo(QString("msg queue : %1").arg(m_msgQueue.count()).toStdString());
    if (!m_msgQueue.isEmpty())
    {
        QByteArray msg;
        while (!m_msgQueue.isEmpty())
        {
            msg += m_msgQueue.dequeue();
        }
        MSGPARSER->processBuffer(msg);
    }

    if (m_runtimes >= 50) // 5秒钟查询一次
    {
        m_runtimes = 0;
        MSGPARSER->detectDevices();
    }
}

void PortManage::moveToThreadEx(QThread *thread)
{
    moveToThread(thread);
}